<?php

class edit_championship
{
	var $mess = '';

	function edit_championship()
	{
		if(isset($_POST['removeItem']) && isset($_POST['itemID']))
		{
			$this->doDelete();
		}
		else if(isset($_POST['addRecord']) && isset($_POST['id']))
		{
			$this->doAdd();
		}
		else if(isset($_POST['editRecord']) && isset($_POST['id']))
		{
			$this->doEdit();
		}
	}

	function render()
	{		
		include_once('includes/addMessage.php');
		
		if($this->mess != '')
			$this->mess->render();
		
		if( ! isset($_POST['removeItem']) && isset($_POST['itemID']))
		{
			$this->renderEdit();
		}
		else
		{
			$this->renderAdd();
		}
	}
	
	function doDelete()
	{
		global $skin_dir, $db, $lang;
		
		//get the record
		$qSel = "select c_title from ". CHAMPIONSHIPS_TABLE . " Where c_id = " . $_POST['itemID'];
		$result = $db->sql_query($qSel);
		if($row = $db->sql_fetchrow($result)) 
		{
			$qDel = "delete from ". CHAMPIONSHIPS_TABLE . " Where c_id = " . $_POST['itemID'];
			$result = $db->sql_query($qDel);
			$this->mess = new addMessage(str_replace('{entry}', $row['c_title'], $lang['AdminRemoved']), $lang['AdminRemovedTitle']);
			
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
			
		}
	}
	
	function doAdd()
	{
		global $db, $lang;
		
		if($this->validateFields(false)) 
		{
			$qUpd = "insert into  ". CHAMPIONSHIPS_TABLE . " (c_smallTitle, c_title, c_introText, c_scoringText, c_meetingText, c_rules, 
							c_eventsPerMeeting, c_handbookURL, c_broadcastLiveURL, c_broadcastDownloadURL, c_modDownloadURL, c_active, c_openSignUps,
							c_enabled, c_carNumbers, c_teamChanging, c_carChanging, c_classChange, c_multipleCarsPerTeam, c_defaultDiv, c_driversPerTeam, 
							c_incidents, c_minRacesToEnter, c_multiDriverEntryMin, c_multiDriverEntryMax, c_serverPassword, c_license, c_rfm, c_stagedevents) values('" .
							$_POST['shortTitle'] . "', '" . $_POST['fullTitle'] . "', '" . nl2br($_POST['intoText']) . "', '" . 
							nl2br($_POST['scoringText']) . "', '" . nl2br($_POST['meetingText']) . 	"', '" . nl2br($_POST['rulesText']) . 
							"', " . $_POST['eventsPerMeeting'] . ", '" . $_POST['handbookURL'] . "', '" . $_POST['broadcastLiveURL'] . "', '" . 
							$_POST['broadcastDownloadURL'] . "', '" . $_POST['modDownloadURL'] . "', " . $_POST['active'] . ", " . 
							$_POST['openSignUps'] . ", " . $_POST['enabled'] .   
							", " . $_POST['carNumbers'] .  ", " . $_POST['teamChanging'] .  ", " . $_POST['carChanging'] .  ", " .  $_POST['classChanging'] .  ", " . 
							$_POST['oneMakePerTeam'] .  ", "  . $_POST['defaultDivision'] . ', ' . $_POST['driversPerTeam'] . ', ' . $_POST['incidents'] . ', ' .
							$_POST['minRacesToEnter'] . ', ' . $_POST['multiDriverEntryMin'] . ', ' . $_POST['multiDriverEntryMax'] . 
							', "' . $_POST['serverPassword'] . '", ' . $_POST['license'] . ', "' . $_POST['modRFM'] . '", ' . $_POST["stagedEvents"] . ')' ;
			$result = $db->sql_query($qUpd);
			
			$this->mess = new addMessage(str_replace('{entry}', $_POST['fullTitle'], $lang['AdminAdded']), $lang['AdminAddedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function doEdit()
	{
		global $db, $lang;
		
		if($this->validateFields(true)) 
		{
			$qUpd = "update  ". CHAMPIONSHIPS_TABLE . " set c_smallTitle = '" . $_POST['shortTitle'] .
													  "', c_title = '" . $_POST['fullTitle'] .
													  "', c_introText = '" . nl2br($_POST['intoText']) .
													  "', c_scoringText = '" . nl2br($_POST['scoringText']) .
													  "', c_meetingText = '" . nl2br($_POST['meetingText']) .
													  "', c_rules = '" . nl2br($_POST['rulesText']) .
													  "', c_eventsPerMeeting = " . $_POST['eventsPerMeeting'] .
													  ", c_handbookURL = '" . $_POST['handbookURL'] .
													  "', c_broadcastLiveURL = '" . $_POST['broadcastLiveURL'] .
													  "', c_broadcastDownloadURL = '" . $_POST['broadcastDownloadURL'] .
													  "', c_modDownloadURL = '" . $_POST['modDownloadURL'] .
													  "', c_active = " . $_POST['active'] .
													  ", c_openSignUps = " . $_POST['openSignUps'] .
													  ", c_enabled = " . $_POST['enabled'] .
													  ", c_carNumbers = " . $_POST['carNumbers'] .
													  ", c_incidents = " . $_POST['incidents'] .
													  ", c_teamChanging = " . $_POST['teamChanging'] .
													  ", c_carChanging = " . $_POST['carChanging'] .
													  ", c_classChange = " . $_POST['classChanging'] .
													  ", c_driversPerTeam = " . $_POST['driversPerTeam'] .
													  ", c_multipleCarsPerTeam = " . $_POST['oneMakePerTeam'] .
													  ", c_defaultDiv = " . $_POST['defaultDivision'] .
													  ", c_minRacesToEnter = " . $_POST['minRacesToEnter'] .
													  ", c_multiDriverEntryMin = " . $_POST['multiDriverEntryMin'] .
													  ", c_multiDriverEntryMax = " . $_POST['multiDriverEntryMax'] .
													  ", c_serverPassword = '" . $_POST['serverPassword'] .
													  "', c_license = " . $_POST['license'] .
                                                                                                          ", c_rfm = '" . $_POST['modRFM'] .
													  "', c_stagedEvents = " . $_POST['stagedEvents'] .
                                                                                                          " WHERE c_id = " . $_POST['id'] ;
			$result = $db->sql_query($qUpd);

			$this->mess = new addMessage(str_replace('{entry}', $_POST['fullTitle'], $lang['AdminUpdated']), $lang['AdminUpdatedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function validateFields($checkID = false)
	{
		$valid = true;
		
		if($checkID)
		{
			$valid = is_numeric($_POST['id']);
		}
		
		
		return $valid && is_numeric($_POST['eventsPerMeeting'])
 					  && is_numeric($_POST['openSignUps'])
 					  && is_numeric($_POST['active'])
 					  && is_numeric($_POST['driversPerTeam'])
					  && is_numeric( $_POST['carChanging'])
					  && is_numeric( $_POST['classChanging'])
					  && is_numeric( $_POST['minRacesToEnter'])
					  && is_numeric( $_POST['multiDriverEntryMin'])
					  && is_numeric( $_POST['multiDriverEntryMax'])
					  && is_numeric( $_POST['stagedEvents'])
					  && is_numeric( $_POST['license'])
					  && $_POST['multiDriverEntryMax'] >= $_POST['multiDriverEntryMin'];
					  
	}
	
	function renderEdit()
	{
		global $skin_dir, $db, $lang;
		
		//get the record
		$q = "Select * From ". CHAMPIONSHIPS_TABLE . " Where c_id = " . $_POST['itemID'];
		$result = $db->sql_query($q);
		if($row = $db->sql_fetchrow($result)) 
		{
			$mess = new addMessage($lang['AdminEdit'], $lang['AdminEditTitle']);
			$mess->render();
			
			$submitValue = 'editRecord';
			$submitText = $lang['SaveButton'];
			
			$activeOptions = '<option value="0" '. ($row['c_active']==0 ? 'SELECTED' : '').'>No</option>' .
							 '<option value="1" '. ($row['c_active']==1 ? 'SELECTED' : '').'>Yes</option>';
		
			$signUpOptions = '<option value="0" '. ($row['c_openSignUps']==0 ? 'SELECTED' : '').'>No</option>' .
							 '<option value="1" '. ($row['c_openSignUps']==1 ? 'SELECTED' : '').'>Yes</option>';
							 		 
			$enabledOptions = '<option value="0"'. ($row['c_enabled']==0 ? 'SELECTED' : '').'>No</option>' .
							 '<option value="1"'. ($row['c_enabled']==1 ? 'SELECTED' : '').'>Yes</option>';
							 
									
			$oneMakePerTeamOptions = '<option value="0" '. ($row['c_multipleCarsPerTeam']==0 ? 'SELECTED' : '').'>No</option>' .
							 '<option value="1"'. ($row['c_multipleCarsPerTeam']==1 ? 'SELECTED' : '').'>Yes</option>';
												 
			$carNumbersOptions = '<option value="0"'. ($row['c_carNumbers']==0 ? 'SELECTED' : '').'>No</option>' .
							 '<option value="1"'. ($row['c_carNumbers']==1 ? 'SELECTED' : '').'>Yes</option>';

			$stagedEventsOptions = '<option value="0"'. ($row['c_stagedEvents']==0 ? 'SELECTED' : '').'>No</option>' .
							 '<option value="1"'. ($row['c_stagedEvents']==1 ? 'SELECTED' : '').'>Yes</option>';

			$teamChangingOptions = '<option value="0"'. ($row['c_teamChanging']==0 ? 'SELECTED' : '').'>'.
																						$lang['ChampionshipTeamChangingOpt0'].'</option>' .
				'<option value="1"'. ($row['c_teamChanging']==1 ? 'SELECTED' : '').'>'.$lang['ChampionshipTeamChangingOpt1'].'</option>'  .
				'<option value="2"'. ($row['c_teamChanging']==2 ? 'SELECTED' : '').'>'.$lang['ChampionshipTeamChangingOpt2'].'</option>' ;
							 
			$carChangingOptions = '<option value="0" '. ($row['c_carChanging']==0 ? 'SELECTED' : '').'>No</option>' .
							 '<option value="1"'. ($row['c_carChanging']==1 ? 'SELECTED' : '').'>Yes</option>';
						 
			$classChangingOptions = '<option value="0" '. ($row['c_classChange']==0 ? 'SELECTED' : '').'>No</option>' .
							 '<option value="1"'. ($row['c_classChange']==1 ? 'SELECTED' : '').'>Yes</option>';
							 
			$incidentOptions = '<option value="0" '. ($row['c_incidents']==0 ? 'SELECTED' : '').'>No</option>' .
							 '<option value="1"'. ($row['c_incidents']==1 ? 'SELECTED' : '').'>Yes</option>';
							 
			$defaultDivisionOptions = '<option value="0" '. ($row['c_defaultDiv']==0 ? 'SELECTED>' : '>') . $lang['Unassigned'].'</option>';
			$q = "Select * From ". DIVISIONS_TABLE . " Where d_champid = " . $_POST['itemID'];
			$result = $db->sql_query($q);
			while($r = $db->sql_fetchrow($result)) 
			{
				$defaultDivisionOptions .= '<option value="' . $r['d_id'] . '"'. ($r['d_id']==$row['c_defaultDiv'] ? ' SELECTED>' : '>') . 
											$r['d_title'].'</option>';
			}
		
			$q = "Select l_id, l_name From ". LICENSES_TABLE . " ORDER BY l_order ASC";
			$licenseOptions = '';
			$result = $db->sql_query($q);
			while($r = $db->sql_fetchrow($result)) 
			{
				$licenseOptions .= '<option value="' . $r['l_id'] . '"'. ($r['l_id']==$row['c_license'] ? ' SELECTED>' : '>') . 
											$r['l_name'].'</option>';
			}
			
			$row['c_introText'] = stripslashes($row['c_introText']);
			$row['c_scoringText'] = stripslashes($row['c_scoringText']);
			$row['c_meetingText'] = stripslashes($row['c_meetingText']);
			$row['c_rules'] = stripslashes($row['c_rules']);
		
			include $skin_dir . 'admin/editchampionship.tpl';
		}
		else
		{
			$mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
			$mess->render();
		}
	}

	function renderAdd()
	{
		global $skin_dir,  $lang, $db;
		
		$mess = new addMessage($lang['AdminAdd'], $lang['AdminAddTitle']);
		$mess->render();
		
		$row['c_id'] = '';
		$row['c_title'] = '';
		$row['c_smallTitle'] = '';
		$row['c_introText'] = '';
		$row['c_scoringText'] = '';
		$row['c_meetingText'] = '';
		$row['c_divisions'] = '';
		$row['c_eventsPerMeeting'] = '';
		$row['c_handbookURL'] = '';
		$row['c_broadcastLiveURL'] = '';
		$row['c_broadcastDownloadURL'] = '';
		$row['c_modDownloadURL'] = '';
		$row['c_logoURL'] = '';
		$row['c_firstRace'] = '';
		$row['c_lastRace'] = '';
		$row['c_driversPerTeam'] = 0;
		$row['c_rules'] = '';
		$row['c_minRacesToEnter'] = 0;
		$row['c_multiDriverEntryMin'] = 0;
		$row['c_multiDriverEntryMax'] = 0;
		$row['c_serverPassword'] = '';
                $row['c_rfm'] = '';
		

		$stagedEventsOptions =  '<option value="0" SELECTED>No</option>' .
						 '<option value="1">Yes</option>';

		$activeOptions = '<option value="0" SELECTED>No</option>' .
						 '<option value="1">Yes</option>';
		
		$incidentOptions = '<option value="0" SELECTED>No</option>' .
						 '<option value="1">Yes</option>';
						 
		$signUpOptions = '<option value="0" SELECTED>No</option>' .
						 '<option value="1">Yes</option>';
		
		$enabledOptions = '<option value="0" SELECTED>No</option>' .
						 '<option value="1">Yes</option>';
						 
						 		
		$oneMakePerTeamOptions = '<option value="0" SELECTED>No</option>' .
						 '<option value="1">Yes</option>';
						 					 
		$carNumbersOptions = '<option value="0" SELECTED>No</option>' .
						 '<option value="1">Yes</option>';
						 
		$teamChangingOptions = '<option value="0" SELECTED>'.$lang['ChampionshipTeamChangingOpt0'].'</option>' .
						 '<option value="1">'.$lang['ChampionshipTeamChangingOpt1'].'</option>'  .
						 '<option value="2">'.$lang['ChampionshipTeamChangingOpt2'].'</option>' ;
						 
		$carChangingOptions = '<option value="0" SELECTED>No</option>' .
						 '<option value="1">Yes</option>';
						 
		$classChangingOptions = '<option value="0" SELECTED>No</option>' .
						 '<option value="1">Yes</option>';
						 
		$defaultDivisionOptions = '<option value="0" SELECTED>'.$lang['Unassigned'].'</option>' ;
			
		$q = "Select l_id, l_name From ". LICENSES_TABLE . " ORDER BY l_order ASC";
		$licenseOptions = '';
		$result = $db->sql_query($q);
		while($r = $db->sql_fetchrow($result)) 
		{
			$licenseOptions .= '<option value="' . $r['l_id'] . '">' . $r['l_name'].'</option>';
		}			 
						 				 
		$submitValue = 'addRecord';
		$submitText = $lang['AddButton'];
		
		include $skin_dir . 'admin/editchampionship.tpl';
	}
	
}
?>